package team.hyznrj.studentsys.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import team.hyznrj.studentsys.dao.mapper.ConductScoreMapper;
import team.hyznrj.studentsys.dao.repository.QualityMarkRepository;
import team.hyznrj.studentsys.entity.QualityMark;
import team.hyznrj.studentsys.entity.table.ScoreQualityTable;
import team.hyznrj.studentsys.enums.MarkEnum;
import team.hyznrj.studentsys.exception.MarkException;
import team.hyznrj.studentsys.service.QualityMarkService;
import team.hyznrj.studentsys.utils.KeyUtil;

import java.util.List;

/**
 * @author ReMidDream
 * @date 2018-04-11 14:09
 **/
@Service
@Slf4j
public class QualityMarkServiceImpl implements QualityMarkService {

    @Autowired
    private QualityMarkRepository qualityMarkRepository;
    @Autowired
    private ConductScoreMapper conductScoreMapper;

    @Override
    public List<QualityMark> findAllQualityMark() {
        return qualityMarkRepository.findAll();
    }

    @Override
    public boolean insertOrUpdateQualityMark(QualityMark qualityMark) {
        //判断是更新还是插入
        if(qualityMark.getId() != null){
            //更新操作
            QualityMark check = qualityMarkRepository.findOne(qualityMark.getId());
            //判断是非存在此实体
            if (check == null) {
                log.error(" @更新综合素质分:不存在实体 id={}",qualityMark.getId());
                throw  new MarkException(MarkEnum.PARAMRMETER_ERROR);
            }
            QualityMark save = qualityMarkRepository.save(qualityMark);
            if (save != null) {
                throw new MarkException(MarkEnum.UPDATE_ERROR);
            }
            return  true;
        }
        //插入操作
        qualityMark.setId(KeyUtil.getUniqueKey());
        QualityMark save = qualityMarkRepository.save(qualityMark);
        if (save != null) {
            throw new MarkException(MarkEnum.UPDATE_ERROR);
        }
        return  true;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean delectQualityMark(List<String> ids) {
        List<ScoreQualityTable> tableList = conductScoreMapper.findScoreQualityTableByQualityMarkIds(ids);
        if (!CollectionUtils.isEmpty(tableList)) {
            log.error(" @删除综合素质分: 该数据已被学生使用,tableList={}",tableList);
            throw  new MarkException(MarkEnum.DATE_USED);
        }
        for (String id : ids) {
            qualityMarkRepository.delete(id);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean forcedDelectQualityMark(List<String> ids) {
        int result = conductScoreMapper.deleteScoreQualityTableByQualityMarkIds(ids);
        if (result < 0) {
            log.error( "@强制删除综合素质分: 数据库异常");
            throw  new MarkException(MarkEnum.DELETE_ERROR);
        }
        for (String id : ids) {
            qualityMarkRepository.delete(id);
        }
        return true;
    }
}
